package com.grasp.custom.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.grasp.activity.R;

public class ButtomDialog extends Dialog {

	private LinearLayout layout;
	private GridView gridView;
	private Window window;

	public ButtomDialog(Context context) {
		super(context);
		init();
	}

	public ButtomDialog(Context context, int theme) {
		super(context, theme);
		init();
	}

	private void init() {
		window = getWindow();
		window.setGravity(Gravity.BOTTOM);
		WindowManager.LayoutParams params = window.getAttributes();
		params.width = window.getAttributes().width;
		params.height = LayoutParams.WRAP_CONTENT;
		window.setAttributes(params);
		layout = new LinearLayout(getContext());
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackgroundColor(Color.WHITE);
		setContentView(layout);

		initTitle();
		initGridView();
		initBottomBtn();
	}

	private void initTitle() {
		TextView titleView = new TextView(getContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(10, 20, 10, 20);
		titleView.setText("选择图标");
		titleView.setTextSize(18);
		titleView.setGravity(Gravity.CENTER);
		titleView.setTextColor(Color.BLACK);
		layout.addView(titleView, params);
		TextView lineView = new TextView(getContext());
		LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lineView.setHeight(2);
		lineView.setBackgroundColor(Color.GRAY);
		layout.addView(lineView, lineParams);
	}

	private void initGridView() {
		gridView = new GridView(getContext());
		gridView.setPadding(10, 5, 10, 5);
		int width = getContext().getResources().getDisplayMetrics().widthPixels;

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		params.weight = 1.0f;
		gridView.setNumColumns(3);
		gridView.setColumnWidth(width);
		gridView.setGravity(Gravity.CENTER);
		gridView.setVerticalSpacing(2);
		gridView.setHorizontalSpacing(2);
		gridView.setCacheColorHint(Color.TRANSPARENT);
		layout.addView(gridView, params);
	}

	private void initBottomBtn() {
		TextView lineView = new TextView(getContext());
		LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		lineView.setHeight(2);
		lineView.setBackgroundColor(Color.GRAY);
		layout.addView(lineView, lineParams);
		Button clearBtnView = new Button(getContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		clearBtnView.setBackgroundResource(R.drawable.clear_button_bg);
		params.setMargins(15, 15, 15, 15);
		clearBtnView.setText("取消");
		clearBtnView.setTextSize(18);
		clearBtnView.setGravity(Gravity.CENTER);
		clearBtnView.setTextColor(Color.BLACK);
		layout.addView(clearBtnView, params);
		clearBtnView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ButtomDialog.this.dismiss();
			}
		});
	}

	public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		gridView.setOnItemClickListener(mOnItemClickListener);
	}

	public void setAdapter(ListAdapter adapter) {
		gridView.setAdapter(adapter);
	}

	public void setDialogAnim(int resId) {
		window.setWindowAnimations(resId);
	}
}
